const db = require('../utils/db')
class PositionModel {
	constructor() {
		this.positionModel = db.model('positions', {
			companyLogo: String,
			companyName: String,
			positionName: String,
			city: String,
			salary: String
		})
	}
	save(data) {
		return this.positionModel.insertMany(data);
	}
	getpos() {
		return this.positionModel.find();
	}
	del(data) {
		return this.positionModel.deleteOne(data);
	}

	//模糊查找数据
	find(data) {
		let {
			pageno,
			pagesize,
			keywords
		} = data;
		pageno -= 1;
		let reg = new RegExp(keywords, 'i');
		return this.positionModel.find({
			$or: [{
				companyName: reg
			}, {
				positionName: reg
			}]
		}).limit(pagesize * 1).skip(pageno * pagesize)
	}
	//查找数据的数量****跟随上方函数的条件进行查找的
	findCount(data) {
		let {
			keywords
		} = data;
		let reg = new RegExp(keywords, 'i');
		return this.positionModel.find({
			$or: [{
				companyName: reg
			}, {
				positionName: reg
			}]
		}).count();
	}

	del(id) {
		return this.positionModel.deleteOne({
			_id: id
		})
	}

	update(id, data) {
		return this.positionModel.updateOne({
			_id: id
		}, data);
	}
	
	findOne(id) {
	    return this.positionModel.find({ _id: id })
	 }
}
module.exports = new PositionModel();
